﻿using System;
using System.Security;
using System.Security.Permissions;

namespace AzManPermissions
{
  /// <summary>
  /// Allows security actions for <see cref="T:AzManPermissions.AzManOperationPermission" /> to be applied to code using declarative security. This class cannot be inherited.
  /// </summary>
  [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
  [Serializable]
  public sealed class AzManOperationPermissionAttribute : CodeAccessSecurityAttribute
  {
    /// <summary>
    /// Initializes a new instance of the <see cref="T:AzManPermissions.AzManOperationPermissionAttribute" /> class with the specified <see cref="T:System.Security.Permissions.SecurityAction" />.
    /// </summary>
    /// <param name="action">One of the <see cref="T:System.Security.Permissions.SecurityAction" /> values.</param>
    public AzManOperationPermissionAttribute(SecurityAction action)
      : base(action)
    {
      Operation = PermissionDescriptor.defaultOperation;
      ScopeName = PermissionDescriptor.defaultScopeName;
      ObjectName = PermissionDescriptor.defaultObjectName;
    }

    /// <summary>
    /// Creates and returns a new <see cref="T:AzManPermissions.AzManOperationPermission" />.
    /// </summary>
    /// <returns>An <see cref="T:AzManPermissions.AzManOperationPermission" /> that corresponds to this attribute.</returns>
    public override IPermission CreatePermission()
    {
      return new AzManOperationPermission(Name, Operation, ScopeName, ObjectName);
    }

    /// <summary>
    /// Gets or sets the name of the identity associated with the current principal.
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// Gets or sets the AzMan operation number.
    /// </summary>
    public int Operation { get; set; }

    /// <summary>
    /// Gets or sets the string used for AzMan run-time auditing.
    /// </summary>
    public string ObjectName { get; set; }

    /// <summary>
    /// The name of a scope as defined in the AzMan store. To use the default application-level scope, set this to an empty string (which is the default value).
    /// </summary>
    public string ScopeName { get; set; }
  }
}